package main

import (
	"github.com/ying32/govcl/vcl"
	"io/ioutil"
	"net/http"
	"time"

	"github.com/buger/jsonparser"
)

const version = "v0.0.0-alpha"

func (f *MainForm) CheckUpdate() {
	checkUrl := "https://gitee.com/api/v5/repos/1967988842/coder-pro/releases/latest?access_token=" + giteeToken
	client := &http.Client{
		Timeout: 5 * time.Second,
	}
	resp, err := client.Get(checkUrl)
	if err != nil {
		return
	}
	defer func() {
		_ = resp.Body.Close()
	}()
	body, _ := ioutil.ReadAll(resp.Body)

	currentTag, err := jsonparser.GetString(body, "tag_name")
	if err != nil {
		return
	}

	if currentTag != version {
		downloadUrl, err := jsonparser.GetString(body, "assets", "[0]", "browser_download_url")
		if err != nil {
			return
		}
		vcl.ThreadSync(func() {
			f.addUpdateMenu(downloadUrl)
		})
	}
}
